// https://www.luogu.com.cn/problem/P1364
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
using ll = long long;
const int inf = 0x3f3f3f3f;
#define rf(i, n) for (int i = 1; i <= (n); ++i)
int rad(); // read int
const int max_size = 5 + 100;
const int maxn = 5 + 100;

int n;
int a[maxn], siz[maxn];
vector<int> adj[maxn];
int f[maxn];
int ans;

int init(int now = 1, int fa = 0, int dep = 0) {
    f[1] += a[now] * dep;
    int cnt = a[now];
    for (auto &p : adj[now])
        if (p != fa)
            cnt += init(p, now, dep + 1);
    return siz[now] = cnt;
}

void dfs(int now = 1, int fa = 0) {
    ans = min(ans, f[now]);
    for (auto &p : adj[now])
        if (p != fa)
            f[p] = f[now] + siz[1] - siz[p] - siz[p], dfs(p, now);
}

int main() {
    n = rad();
    rf(i, n) {
        a[i] = rad();
        int l = rad(), r = rad();
        if (l > 0) adj[i].push_back(l), adj[l].push_back(i);
        if (r > 0) adj[i].push_back(r), adj[r].push_back(i);
    }
    init();
    ans = inf;
    dfs(1);
    printf("%d", ans);
}

int rad() {
    int back = 0, ch = 0, neg = 0;
    for (; ch < '0' || ch > '9'; ch = getchar())
        neg = ch == '-';
    for (; ch >= '0' && ch <= '9'; ch = getchar())
        back = (back << 1) + (back << 3) + (ch & 0xf);
    return (back ^ -neg) + neg;
}